// pages/huxing_info/index.js
const app = getApp();
const amap = require('../../utils/amap-wx.js');
const key = app.globalData.key;
let globalData = getApp().globalData;
var myAmap = new amap.AMapWX({ key: key });
Page({
    /**
     * 页面的初始数据
     */
    data: {
        currentId: '2',
        activeIndex: 0,
        banner_url: '../../images/huxing.jpg',
        section: app.globalData.section,
        qian_img: globalData.qian_img,
        huxing_info: {},
        other: [],
        phone: "123456789",
        procurrentId: '1',
        aroundList: globalData.aroundList,
        status: null,
        isShow: false,
        markers: [],
        points: [],
        markersData: [],
        scale:13,
        location: '',
        name: '',
        address: ''
    },
    //点击每个导航的点击事件
    handleTap: getApp().globalData.handleTap,
    bindFullview: function () {
        // let url = this.data.huxing_info.view_url;
        let url = encodeURIComponent(this.data.huxing_info.view_url);
        
        console.log(typeof(url));
        wx.navigateTo({
            url: '../fullview/index?url=' + url,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        this.getData(options.id);
        // 页面加载获取当前定位位置为地图的中心坐标
        
    },
    getType(e) {//获取选择的附近关键词，同时更新状态
      this.setData({status: e.currentTarget.dataset.type})
      this.getAround(e.currentTarget.dataset.keywords, e.currentTarget.dataset.type, e.currentTarget.dataset.id);
    },
    getAround(keywords, types,ids) {//通过关键词获取附近的点，只取前十个，同时保证十个点在地图中显示
        var _this = this;
        myAmap.getPoiAround({
            iconPath: _this.data.aroundList[ids].iconPath,
            querykeywords: keywords,
            querytypes: types,
            location: _this.data.location,
            radius: 5000,
            success(data) {
                if (data.markers) {
                    var markers = [], points = [],
                     markersData = _this.data.markersData;
                    for (var value of data.markers) {
                        if (value.id > 4) break;
                        if (value.id == 0) {
                            _this.setData({
                                name: value.name,
                                address: value.address,
                                isShow: true
                            })
                        }
                        markers.push({
                            id: value.id,
                            latitude: value.latitude,
                            longitude: value.longitude,
                            title: value.name,
                          iconPath: _this.data.aroundList[ids].iconPath,
                            width: 20,
                            height: 23,
                            anchor: { x: .5, y: 1 },
                            label: {
                                content: value.name,
                                color: '#000',
                                fontSize: 12,
                                borderRadius: 5,
                                bgColor: '#fff',
                                padding: 3,
                                x: 0,
                                y: -50,
                                textAlign: 'center'
                            }
                        });
                        points.push({
                            latitude: value.latitude,
                            longitude: value.longitude
                        })
                    }
                    var markersAll = markers.concat(markersData);
                    _this.setData({
                      markers: markersAll,
                        points: points
                    })
                }
            },
            fail: function (info) {
                wx.showToast({ title: 'cuowu' })
            },
        })
    },
    //自定义的banner图的方法
    changeImg(e) {
        let that = this
        that.activeIndex = e.detail.current
        this.setData({
            activeIndex: e.detail.current
        })
    },
    showLength() {
        return this.swiperList.length
    },
    //房贷计算器
    fangDaiBindTap() {
        wx.navigateTo({
            url: '../interestRate/index',
        })
    },
    //楼栋分布
    louDongBindTap() {
        wx.navigateTo({
            url: '../loudong-webview/index?pid='+globalData.pid,
        })
    },
    //在线订购
    dinggouBindTap(){
      wx.navigateTo({
        url: '../payPlan/index',
      });
    },
    //免费咨询
    freeTell: function (e) {
        let item = e.currentTarget.dataset.item;
        wx.makePhoneCall({
            phoneNumber: item,
        })
    },
    //户型信息
    huxingTap: function (e) {
        let id = e.currentTarget.id;
        if (id) {
            this.setData({
                procurrentId: id
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getLocal(){
      var _this = this;
      myAmap.getRegeo({
        success: function (data) {
          var p_name = _this.data.huxing_info.p_name;
          var p_lat = _this.data.huxing_info.lat;
          var p_lng = _this.data.huxing_info.lng;
          _this.setData({
            markers: [{
              iconPath: "../../images/icon/icon-dingwei.png",
              latitude: p_lat,
              longitude: p_lng,
              width: 32,
              height: 32,
              label: {
                content: p_name,
                color: '#000',
                fontSize: 12,
                borderRadius: 5,
                bgColor: '#fff',
                padding: 6,
                textAlign: 'center'
              }
            }],
            markersData: [{
              iconPath: "../../images/icon/icon-dingwei.png",
              latitude: p_lat,
              longitude: p_lng,
              width: 32,
              height: 32,
              label: {
                content: p_name,
                color: '#000',
                fontSize: 12,
                borderRadius: 5,
                bgColor: '#fff',
                padding: 6,
                textAlign: 'center'
              }
            }],
          });
        },
        fail: function (info) {
          var p_name = _this.data.huxing_info.p_name;
          var p_lat = _this.data.huxing_info.lat;
          var p_lng = _this.data.huxing_info.lng;
          _this.setData({
            markers: [{
              iconPath: "../../images/icon/icon-dingwei.png",
              latitude: p_lat,
              longitude: p_lng,
              width: 32,
              height: 32,
              label: {
                content: p_name,
                color: '#000',
                fontSize: 12,
                borderRadius: 5,
                bgColor: '#fff',
                padding: 6,
                textAlign: 'center'
              }
            }],
            markersData: [{
              iconPath: "../../images/icon/icon-dingwei.png",
              latitude: p_lat,
              longitude: p_lng,
              width: 32,
              height: 32,
              label: {
                content: p_name,
                color: '#000',
                fontSize: 12,
                borderRadius: 5,
                bgColor: '#fff',
                padding: 6,
                textAlign: 'center'
              }
            }],
          });
        }
      })
    },
    getData(id) { 
        let data = {
            id: id ? id : 1
        }
        globalData.sendPost('house/detail', data).then(res => {
            if (res.status == 1) {
                this.setData({
                  huxing_info: res.data,
                  other:res.other,
                  location: res.data.lat + ',' + res.data.lng
                })
                this.getLocal();
            }
        })
    },
    toDetail(event){
        let id=event.currentTarget.dataset.id;
        wx.pageScrollTo({
            scrollTop: 0,
        })
        this.getData(id);
    }
})